package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.corundumstudio.socketio.SocketIOServer;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.business.common.Constants;
import com.lvy.hczn.front.business.dto.ElectronicWeightDto;
import com.lvy.hczn.front.business.dto.MessageInfo;
import com.lvy.hczn.front.business.service.ElectronicWeightService;
import com.lvy.hczn.front.common.core.domain.entity.SysDictData;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

/**
 * @description: 电子秤散装/分拣/报损称重
 * @projectName: wms-front
 * @package: com.lvy.hczn.front.business.service.impl
 * @className: ElectronicWeightServiceImpl
 * @author: yw
 * @date: 2024/5/16 17:23
 */
@Service
@Slf4j
public class ElectronicWeightServiceImpl implements ElectronicWeightService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SocketIOServer socketIOServer;

    /**
     * @Description 电子秤散装/分拣/报损称重topic返回
     * @Param [event, msg, topic]
     * @Author yw
     * @Date 2024/5/16 17:31
     * @Return void
     **/
    @Override
    public void electronicWeightInfo(String event, String msg, String topic) {
        log.error("电子秤散装/分拣/报损称重数据返回："+ msg);
        ElectronicWeightDto electronicWeightDto = JSON.parseObject(msg, ElectronicWeightDto.class);
        if (StrUtil.isEmpty(electronicWeightDto.getIp())) {
            log.error("电子秤散装/分拣/报损称重ip为空");
            throw new UtilException("电子秤散装/分拣/报损称重ip为空");
        }
        if (electronicWeightDto.getWeight() == null) {
            log.error("电子秤散装/分拣/报损称重重量为空");
            throw new UtilException("电子秤散装/分拣/报损称重重量为空");
        }

        JSONArray array = (JSONArray) redisCache.get(CacheConstants.DICT_TYPE_WH_COM);

        //存在在数据字典中，则发送到前端
        boolean exist = false;
        SysDictData sysDictData = new SysDictData();
        /**获取上一个下架货箱的仓库index**/
        for (int i = 0; i < array.size(); i++) {
            SysDictData data = (SysDictData) array.get(i);
            if (electronicWeightDto.getIp().equals(data.getDictLabel().replaceAll(" ", ""))) {
                exist = true;
                sysDictData = data;
                break;
            }
        }

        if (exist && !StrUtil.isEmpty(sysDictData.getDictValue())) {
            BigDecimal weight = new BigDecimal(electronicWeightDto.getWeight()).multiply(new BigDecimal(Constants.WCS_WEIGHT_UNIT_COEFFICIENT));
            electronicWeightDto.setWeight(String.valueOf(weight));
            Object object = redisCache.get(CacheConstants.REDIS_KEY_SOCKET_CLIENT + ":" + sysDictData.getDictValue());
            socketIOServer.getClient(UUID.fromString(String.valueOf(object))).sendEvent(event, JSON.toJSONString(electronicWeightDto));
        }
    }
}
